home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume6 / rpc2 / part06 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  53.6 KB

  1. Subject:  v06i094:  Sun RPC Source (rpc2), Part06/11
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.UUCP
  4.  
  5. Submitted by: cca!SUN.COM!marks (Mark Stein)
  6. Mod.sources: Volume 6, Issue 94
  7. Archive-name: rpc2/Part06
  8.  
  9.  
  10. Sun RPC source (part 6 of 11).  This software package contains code
  11. and documentation for Revision 3.0 of the Sun Remote Procedure Call
  12. library.  In addition, a beta version of the XDR/RPC protocol compiler
  13. is included.  Comments about this latest release may be mailed to
  14. sun!rpc or rpc@sun.com.
  15.  
  16. Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  17. unrestricted use provided that this legend is included on all tape
  18. media and as a part of the software program in whole or part.  Users
  19. may copy or modify Sun RPC without charge, but are not authorized to
  20. license or distribute it to anyone else except as part of a product or
  21. program developed by the user.
  22.  
  23. - - - - - - - - - C U T - H E R E - - - - - - - - - - - - - - - - - -
  24. #! /bin/sh
  25. # This is a shell archive, meaning:
  26. # 1. Remove everything above the #! /bin/sh line.
  27. # 2. Save the resulting text in a file.
  28. # 3. Execute the file with /bin/sh (not csh) to create:
  29. #    rpc/rpclib/auth_none.c
  30. #    rpc/rpclib/auth_unix.c
  31. #    rpc/rpclib/authunix_prot.c
  32. #    rpc/rpclib/clnt_perror.c
  33. #    rpc/rpclib/clnt_raw.c
  34. #    rpc/rpclib/clnt_simple.c
  35. #    rpc/rpclib/clnt_tcp.c
  36. #    rpc/rpclib/clnt_udp.c
  37. # This archive created: Mon Jul 14 16:55:20 1986
  38. export PATH; PATH=/bin:/usr/bin:$PATH
  39. for d in rpc rpc/doc rpc/rpclib rpc/tools rpc/toys rpc/rpclib/profiled rpc/rpcgen rpc/rpcgen/test
  40. do
  41.     if test ! -d $d
  42.     then
  43.         echo "shar: Making directory $d"
  44.         mkdir $d
  45.         chmod 755 $d
  46.     fi
  47. done
  48. echo shar: "extracting 'rpc/rpclib/auth_none.c'" '(2876 characters)'
  49. if test -f 'rpc/rpclib/auth_none.c'
  50. then
  51.     echo shar: "will not over-write existing file 'rpc/rpclib/auth_none.c'"
  52. else
  53. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/auth_none.c'
  54. X/*
  55. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  56. X * unrestricted use provided that this legend is included on all tape
  57. X * media and as a part of the software program in whole or part.  Users
  58. X * may copy or modify Sun RPC without charge, but are not authorized
  59. X * to license or distribute it to anyone else except as part of a product or
  60. X * program developed by the user.
  61. X * 
  62. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  63. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  64. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  65. X * 
  66. X * Sun RPC is provided with no support and without any obligation on the
  67. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  68. X * modification or enhancement.
  69. X * 
  70. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  71. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  72. X * OR ANY PART THEREOF.
  73. X * 
  74. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  75. X * or profits or other special, indirect and consequential damages, even if
  76. X * Sun has been advised of the possibility of such damages.
  77. X * 
  78. X * Sun Microsystems, Inc.
  79. X * 2550 Garcia Avenue
  80. X * Mountain View, California  94043
  81. X */
  82. X#ifndef lint
  83. Xstatic char sccsid[] = "@(#)auth_none.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  84. X#endif
  85. X
  86. X/*
  87. X * auth_none.c
  88. X * Creates a client authentication handle for passing "null" 
  89. X * credentials and verifiers to remote systems. 
  90. X * 
  91. X * Copyright (C) 1984, Sun Microsystems, Inc. 
  92. X */
  93. X
  94. X#include "types.h"
  95. X#include "xdr.h"
  96. X#include "auth.h"
  97. X#define NULL ((caddr_t)0)
  98. X#define MAX_MARSHEL_SIZE 20
  99. X
  100. X/*
  101. X * Authenticator operations routines
  102. X */
  103. Xstatic void    authnone_verf();
  104. Xstatic void    authnone_destroy();
  105. Xstatic bool_t    authnone_marshal();
  106. Xstatic bool_t    authnone_validate();
  107. Xstatic bool_t    authnone_refresh();
  108. X
  109. Xstatic struct auth_ops ops = {
  110. X    authnone_verf,
  111. X    authnone_marshal,
  112. X    authnone_validate,
  113. X    authnone_refresh,
  114. X    authnone_destroy
  115. X};
  116. X
  117. Xstatic AUTH    no_client;
  118. Xstatic char    marshalled_client[MAX_MARSHEL_SIZE];
  119. Xstatic u_int    mcnt;
  120. X
  121. XAUTH *
  122. Xauthnone_create()
  123. X{
  124. X    XDR xdr_stream;
  125. X    register XDR *xdrs;
  126. X
  127. X    if (! mcnt) {
  128. X        no_client.ah_cred = no_client.ah_verf = _null_auth;
  129. X        no_client.ah_ops = &ops;
  130. X        xdrs = &xdr_stream;
  131. X        xdrmem_create(xdrs, marshalled_client, (u_int)MAX_MARSHEL_SIZE,
  132. X            XDR_ENCODE);
  133. X        (void)xdr_opaque_auth(xdrs, &no_client.ah_cred);
  134. X        (void)xdr_opaque_auth(xdrs, &no_client.ah_verf);
  135. X        mcnt = XDR_GETPOS(xdrs);
  136. X        XDR_DESTROY(xdrs);
  137. X    }
  138. X    return (&no_client);
  139. X}
  140. X
  141. X/*ARGSUSED*/
  142. Xstatic bool_t
  143. Xauthnone_marshal(client, xdrs)
  144. X    AUTH *client;
  145. X    XDR *xdrs;
  146. X{
  147. X
  148. X    return ((*xdrs->x_ops->x_putbytes)(xdrs, marshalled_client, mcnt));
  149. X}
  150. X
  151. Xstatic void 
  152. Xauthnone_verf()
  153. X{
  154. X}
  155. X
  156. Xstatic bool_t
  157. Xauthnone_validate()
  158. X{
  159. X
  160. X    return (TRUE);
  161. X}
  162. X
  163. Xstatic bool_t
  164. Xauthnone_refresh()
  165. X{
  166. X
  167. X    return (FALSE);
  168. X}
  169. X
  170. Xstatic void
  171. Xauthnone_destroy()
  172. X{
  173. X}
  174. SHAR_EOF
  175. if test 2876 -ne "`wc -c < 'rpc/rpclib/auth_none.c'`"
  176. then
  177.     echo shar: "error transmitting 'rpc/rpclib/auth_none.c'" '(should have been 2876 characters)'
  178. fi
  179. chmod 444 'rpc/rpclib/auth_none.c'
  180. fi
  181. echo shar: "extracting 'rpc/rpclib/auth_unix.c'" '(8022 characters)'
  182. if test -f 'rpc/rpclib/auth_unix.c'
  183. then
  184.     echo shar: "will not over-write existing file 'rpc/rpclib/auth_unix.c'"
  185. else
  186. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/auth_unix.c'
  187. X/*
  188. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  189. X * unrestricted use provided that this legend is included on all tape
  190. X * media and as a part of the software program in whole or part.  Users
  191. X * may copy or modify Sun RPC without charge, but are not authorized
  192. X * to license or distribute it to anyone else except as part of a product or
  193. X * program developed by the user.
  194. X * 
  195. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  196. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  197. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  198. X * 
  199. X * Sun RPC is provided with no support and without any obligation on the
  200. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  201. X * modification or enhancement.
  202. X * 
  203. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  204. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  205. X * OR ANY PART THEREOF.
  206. X * 
  207. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  208. X * or profits or other special, indirect and consequential damages, even if
  209. X * Sun has been advised of the possibility of such damages.
  210. X * 
  211. X * Sun Microsystems, Inc.
  212. X * 2550 Garcia Avenue
  213. X * Mountain View, California  94043
  214. X */
  215. X#ifndef lint
  216. Xstatic char sccsid[] = "@(#)auth_unix.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  217. X#endif
  218. X
  219. X/*
  220. X * auth_unix.c, Implements UNIX style authentication parameters. 
  221. X *  
  222. X * Copyright (C) 1984, Sun Microsystems, Inc. 
  223. X *
  224. X * The system is very weak.  The client uses no encryption for it's
  225. X * credentials and only sends null verifiers.  The server sends backs
  226. X * null verifiers or optionally a verifier that suggests a new short hand
  227. X * for the credentials.
  228. X *
  229. X */
  230. X
  231. X#include <stdio.h>
  232. X#include "types.h"
  233. X#include <sys/time.h>
  234. X#include "xdr.h"
  235. X#include "auth.h"
  236. X#include "auth_unix.h"
  237. Xchar *malloc();
  238. X
  239. X/*
  240. X * Unix authenticator operations vector
  241. X */
  242. Xstatic void    authunix_nextverf();
  243. Xstatic bool_t    authunix_marshal();
  244. Xstatic bool_t    authunix_validate();
  245. Xstatic bool_t    authunix_refresh();
  246. Xstatic void    authunix_destroy();
  247. X
  248. Xstatic struct auth_ops auth_unix_ops = {
  249. X    authunix_nextverf,
  250. X    authunix_marshal,
  251. X    authunix_validate,
  252. X    authunix_refresh,
  253. X    authunix_destroy
  254. X};
  255. X
  256. X/*
  257. X * This struct is pointed to by the ah_private field of an auth_handle.
  258. X */
  259. Xstruct audata {
  260. X    struct opaque_auth    au_origcred;    /* original credentials */
  261. X    struct opaque_auth    au_shcred;    /* short hand cred */
  262. X    u_long            au_shfaults;    /* short hand cache faults */
  263. X    char            au_marshed[MAX_AUTH_BYTES];
  264. X    u_int            au_mpos;    /* xdr pos at end of marshed */
  265. X};
  266. X#define    AUTH_PRIVATE(auth)    ((struct audata *)auth->ah_private)
  267. X
  268. Xstatic bool_t marshal_new_auth();
  269. X
  270. X
  271. X/*
  272. X * Create a unix style authenticator.
  273. X * Returns an auth handle with the given stuff in it.
  274. X */
  275. XAUTH *
  276. Xauthunix_create(machname, uid, gid, len, aup_gids)
  277. X    char *machname;
  278. X    int uid;
  279. X    int gid;
  280. X    register int len;
  281. X    int *aup_gids;
  282. X{
  283. X    struct authunix_parms aup;
  284. X    char mymem[MAX_AUTH_BYTES];
  285. X    struct timeval now;
  286. X    XDR xdrs;
  287. X    register AUTH *auth;
  288. X    register struct audata *au;
  289. X
  290. X    /*
  291. X     * Allocate and set up auth handle
  292. X     */
  293. X    auth = (AUTH *)mem_alloc(sizeof(*auth));
  294. X#ifndef KERNEL
  295. X    if (auth == NULL) {
  296. X        fprintf(stderr, "authunix_create: out of memory\n");
  297. X        return (NULL);
  298. X    }
  299. X#endif
  300. X    au = (struct audata *)mem_alloc(sizeof(*au));
  301. X#ifndef KERNEL
  302. X    if (au == NULL) {
  303. X        fprintf(stderr, "authunix_create: out of memory\n");
  304. X        return (NULL);
  305. X    }
  306. X#endif
  307. X    auth->ah_ops = &auth_unix_ops;
  308. X    auth->ah_private = (caddr_t)au;
  309. X    auth->ah_verf = au->au_shcred = _null_auth;
  310. X    au->au_shfaults = 0;
  311. X
  312. X    /*
  313. X     * fill in param struct from the given params
  314. X     */
  315. X    (void)gettimeofday(&now,  (struct timezone *)0);
  316. X    aup.aup_time = now.tv_sec;
  317. X    aup.aup_machname = machname;
  318. X    aup.aup_uid = uid;
  319. X    aup.aup_gid = gid;
  320. X    aup.aup_len = (u_int)len;
  321. X    aup.aup_gids = aup_gids;
  322. X
  323. X    /*
  324. X     * Serialize the parameters into origcred
  325. X     */
  326. X    xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  327. X    if (! xdr_authunix_parms(&xdrs, &aup)) 
  328. X        abort();
  329. X    au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  330. X    au->au_origcred.oa_flavor = AUTH_UNIX;
  331. X#ifdef KERNEL
  332. X    au->au_origcred.oa_base = mem_alloc(len);
  333. X#else
  334. X    if ((au->au_origcred.oa_base = mem_alloc(len)) == NULL) {
  335. X        fprintf(stderr, "authunix_create: out of memory\n");
  336. X        return (NULL);
  337. X    }
  338. X#endif
  339. X    bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
  340. X
  341. X    /*
  342. X     * set auth handle to reflect new cred.
  343. X     */
  344. X    auth->ah_cred = au->au_origcred;
  345. X    marshal_new_auth(auth);
  346. X    return (auth);
  347. X}
  348. X
  349. X/*
  350. X * Returns an auth handle with parameters determined by doing lots of
  351. X * syscalls.
  352. X */
  353. XAUTH *
  354. Xauthunix_create_default()
  355. X{
  356. X    register int len;
  357. X    char machname[MAX_MACHINE_NAME + 1];
  358. X    register int uid;
  359. X    register int gid;
  360. X    int gids[NGRPS];
  361. X
  362. X    if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  363. X        abort();
  364. X    machname[MAX_MACHINE_NAME] = 0;
  365. X    uid = geteuid();
  366. X    gid = getegid();
  367. X    if ((len = getgroups(NGRPS, gids)) < 0)
  368. X        abort();
  369. X    return (authunix_create(machname, uid, gid, len, gids));
  370. X}
  371. X
  372. X/*
  373. X * authunix operations
  374. X */
  375. X
  376. Xstatic void
  377. Xauthunix_nextverf(auth)
  378. X    AUTH *auth;
  379. X{
  380. X    /* no action necessary */
  381. X}
  382. X
  383. Xstatic bool_t
  384. Xauthunix_marshal(auth, xdrs)
  385. X    AUTH *auth;
  386. X    XDR *xdrs;
  387. X{
  388. X    register struct audata *au = AUTH_PRIVATE(auth);
  389. X
  390. X    return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  391. X}
  392. X
  393. Xstatic bool_t
  394. Xauthunix_validate(auth, verf)
  395. X    register AUTH *auth;
  396. X    struct opaque_auth verf;
  397. X{
  398. X    register struct audata *au;
  399. X    XDR xdrs;
  400. X
  401. X    if (verf.oa_flavor == AUTH_SHORT) {
  402. X        au = AUTH_PRIVATE(auth);
  403. X        xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
  404. X
  405. X        if (au->au_shcred.oa_base != NULL) {
  406. X            mem_free(au->au_shcred.oa_base,
  407. X                au->au_shcred.oa_length);
  408. X            au->au_shcred.oa_base = NULL;
  409. X        }
  410. X        if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  411. X            auth->ah_cred = au->au_shcred;
  412. X        } else {
  413. X            xdrs.x_op = XDR_FREE;
  414. X            (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
  415. X            au->au_shcred.oa_base = NULL;
  416. X            auth->ah_cred = au->au_origcred;
  417. X        }
  418. X        marshal_new_auth(auth);
  419. X    }
  420. X    return (TRUE);
  421. X}
  422. X
  423. Xstatic bool_t
  424. Xauthunix_refresh(auth)
  425. X    register AUTH *auth;
  426. X{
  427. X    register struct audata *au = AUTH_PRIVATE(auth);
  428. X    struct authunix_parms aup;
  429. X    struct timeval now;
  430. X    XDR xdrs;
  431. X    register int stat;
  432. X
  433. X    if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  434. X        /* there is no hope.  Punt */
  435. X        return (FALSE);
  436. X    }
  437. X    au->au_shfaults ++;
  438. X
  439. X    /* first deserialize the creds back into a struct authunix_parms */
  440. X    aup.aup_machname = NULL;
  441. X    aup.aup_gids = (int *)NULL;
  442. X    xdrmem_create(&xdrs, au->au_origcred.oa_base,
  443. X        au->au_origcred.oa_length, XDR_DECODE);
  444. X    stat = xdr_authunix_parms(&xdrs, &aup);
  445. X    if (! stat) 
  446. X        goto done;
  447. X
  448. X    /* update the time and serialize in place */
  449. X    (void)gettimeofday(&now, (struct timezone *)0);
  450. X    aup.aup_time = now.tv_sec;
  451. X    xdrs.x_op = XDR_ENCODE;
  452. X    XDR_SETPOS(&xdrs, 0);
  453. X    stat = xdr_authunix_parms(&xdrs, &aup);
  454. X    if (! stat)
  455. X        goto done;
  456. X    auth->ah_cred = au->au_origcred;
  457. X    marshal_new_auth(auth);
  458. Xdone:
  459. X    /* free the struct authunix_parms created by deserializing */
  460. X    xdrs.x_op = XDR_FREE;
  461. X    (void)xdr_authunix_parms(&xdrs, &aup);
  462. X    XDR_DESTROY(&xdrs);
  463. X    return (stat);
  464. X}
  465. X
  466. Xstatic void
  467. Xauthunix_destroy(auth)
  468. X    register AUTH *auth;
  469. X{
  470. X    register struct audata *au = AUTH_PRIVATE(auth);
  471. X
  472. X    mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  473. X
  474. X    if (au->au_shcred.oa_base != NULL)
  475. X        mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  476. X
  477. X    mem_free(auth->ah_private, sizeof(struct audata));
  478. X
  479. X    if (auth->ah_verf.oa_base != NULL)
  480. X        mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  481. X
  482. X    mem_free((caddr_t)auth, sizeof(*auth));
  483. X}
  484. X
  485. X/*
  486. X * Marshals (pre-serializes) an auth struct.
  487. X * sets private data, au_marshed and au_mpos
  488. X */
  489. Xstatic bool_t
  490. Xmarshal_new_auth(auth)
  491. X    register AUTH *auth;
  492. X{
  493. X    XDR        xdr_stream;
  494. X    register XDR    *xdrs = &xdr_stream;
  495. X    register struct audata *au = AUTH_PRIVATE(auth);
  496. X
  497. X    xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  498. X    if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  499. X        (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  500. X        perror("auth_none.c - Fatal marshalling problem");
  501. X    } else {
  502. X        au->au_mpos = XDR_GETPOS(xdrs);
  503. X    }
  504. X    XDR_DESTROY(xdrs);
  505. X}
  506. SHAR_EOF
  507. if test 8022 -ne "`wc -c < 'rpc/rpclib/auth_unix.c'`"
  508. then
  509.     echo shar: "error transmitting 'rpc/rpclib/auth_unix.c'" '(should have been 8022 characters)'
  510. fi
  511. chmod 444 'rpc/rpclib/auth_unix.c'
  512. fi
  513. echo shar: "extracting 'rpc/rpclib/authunix_prot.c'" '(2045 characters)'
  514. if test -f 'rpc/rpclib/authunix_prot.c'
  515. then
  516.     echo shar: "will not over-write existing file 'rpc/rpclib/authunix_prot.c'"
  517. else
  518. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/authunix_prot.c'
  519. X/*
  520. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  521. X * unrestricted use provided that this legend is included on all tape
  522. X * media and as a part of the software program in whole or part.  Users
  523. X * may copy or modify Sun RPC without charge, but are not authorized
  524. X * to license or distribute it to anyone else except as part of a product or
  525. X * program developed by the user.
  526. X * 
  527. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  528. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  529. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  530. X * 
  531. X * Sun RPC is provided with no support and without any obligation on the
  532. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  533. X * modification or enhancement.
  534. X * 
  535. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  536. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  537. X * OR ANY PART THEREOF.
  538. X * 
  539. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  540. X * or profits or other special, indirect and consequential damages, even if
  541. X * Sun has been advised of the possibility of such damages.
  542. X * 
  543. X * Sun Microsystems, Inc.
  544. X * 2550 Garcia Avenue
  545. X * Mountain View, California  94043
  546. X */
  547. X#ifndef lint
  548. Xstatic char sccsid[] = "@(#)authunix_prot.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  549. X#endif
  550. X
  551. X/*
  552. X * authunix_prot.c
  553. X * XDR for UNIX style authentication parameters for RPC
  554. X *
  555. X * Copyright (C) 1984, Sun Microsystems, Inc.
  556. X */
  557. X
  558. X#include "types.h"
  559. X#include "xdr.h"
  560. X#include "auth.h"
  561. X#include "auth_unix.h"
  562. X
  563. X/*
  564. X * XDR for unix authentication parameters.
  565. X */
  566. Xbool_t
  567. Xxdr_authunix_parms(xdrs, p)
  568. X    register XDR *xdrs;
  569. X    register struct authunix_parms *p;
  570. X{
  571. X
  572. X    if (xdr_u_long(xdrs, &(p->aup_time))
  573. X        && xdr_string(xdrs, &(p->aup_machname), MAX_MACHINE_NAME)
  574. X        && xdr_int(xdrs, &(p->aup_uid))
  575. X        && xdr_int(xdrs, &(p->aup_gid))
  576. X        && xdr_array(xdrs, (caddr_t *)&(p->aup_gids),
  577. X            &(p->aup_len), NGRPS, sizeof(int), xdr_int) ) {
  578. X        return (TRUE);
  579. X    }
  580. X    return (FALSE);
  581. X}
  582. X
  583. SHAR_EOF
  584. if test 2045 -ne "`wc -c < 'rpc/rpclib/authunix_prot.c'`"
  585. then
  586.     echo shar: "error transmitting 'rpc/rpclib/authunix_prot.c'" '(should have been 2045 characters)'
  587. fi
  588. chmod 444 'rpc/rpclib/authunix_prot.c'
  589. fi
  590. echo shar: "extracting 'rpc/rpclib/clnt_perror.c'" '(5331 characters)'
  591. if test -f 'rpc/rpclib/clnt_perror.c'
  592. then
  593.     echo shar: "will not over-write existing file 'rpc/rpclib/clnt_perror.c'"
  594. else
  595. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/clnt_perror.c'
  596. X/*
  597. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  598. X * unrestricted use provided that this legend is included on all tape
  599. X * media and as a part of the software program in whole or part.  Users
  600. X * may copy or modify Sun RPC without charge, but are not authorized
  601. X * to license or distribute it to anyone else except as part of a product or
  602. X * program developed by the user.
  603. X * 
  604. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  605. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  606. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  607. X * 
  608. X * Sun RPC is provided with no support and without any obligation on the
  609. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  610. X * modification or enhancement.
  611. X * 
  612. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  613. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  614. X * OR ANY PART THEREOF.
  615. X * 
  616. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  617. X * or profits or other special, indirect and consequential damages, even if
  618. X * Sun has been advised of the possibility of such damages.
  619. X * 
  620. X * Sun Microsystems, Inc.
  621. X * 2550 Garcia Avenue
  622. X * Mountain View, California  94043
  623. X */
  624. X#ifndef lint
  625. Xstatic char sccsid[] = "@(#)clnt_perror.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  626. X#endif
  627. X
  628. X/*
  629. X * clnt_perror.c
  630. X *
  631. X * Copyright (C) 1984, Sun Microsystems, Inc.
  632. X *
  633. X */
  634. X#include "types.h"
  635. X#include "xdr.h"
  636. X#include "auth.h"
  637. X#include "clnt.h"
  638. X#include "rpc_msg.h"
  639. X#include <stdio.h>
  640. Xextern char *sys_errlist[];
  641. X
  642. X/*
  643. X * Print reply error info
  644. X */
  645. Xvoid
  646. Xclnt_perror(rpch, s)
  647. X    CLIENT *rpch;
  648. X    char *s;
  649. X{
  650. X    struct rpc_err e;
  651. X    void clnt_perrno();
  652. X
  653. X    CLNT_GETERR(rpch, &e);
  654. X    fprintf(stderr, "%s: ", s);
  655. X    switch (e.re_status) {
  656. X        case RPC_SUCCESS:
  657. X        case RPC_CANTENCODEARGS:
  658. X        case RPC_CANTDECODERES:
  659. X        case RPC_TIMEDOUT:
  660. X        case RPC_PROGUNAVAIL:
  661. X        case RPC_PROCUNAVAIL:
  662. X        case RPC_CANTDECODEARGS:
  663. X            clnt_perrno(e.re_status);
  664. X            break;
  665. X        case RPC_CANTSEND:
  666. X            clnt_perrno(e.re_status);
  667. X            fprintf(stderr, "; errno = %s",
  668. X                sys_errlist[e.re_errno]);
  669. X            break;
  670. X    
  671. X        case RPC_CANTRECV:
  672. X            clnt_perrno(e.re_status);
  673. X            fprintf(stderr, "; errno = %s",
  674. X                sys_errlist[e.re_errno]);
  675. X            break;
  676. X    
  677. X        case RPC_VERSMISMATCH:
  678. X            clnt_perrno(e.re_status);
  679. X            fprintf(stderr, "; low version = %lu, high version = %lu", e.re_vers.low, e.re_vers.high);
  680. X            break;
  681. X    
  682. X        case RPC_AUTHERROR:
  683. X            clnt_perrno(e.re_status);
  684. X            fprintf(stderr, "; why = ");
  685. X            switch (e.re_why) {
  686. X            case AUTH_OK:
  687. X                fprintf(stderr, "AUTH_OK");
  688. X                break;
  689. X    
  690. X            case AUTH_BADCRED:
  691. X                fprintf(stderr, "AUTH_BOGUS_CREDENTIAL");
  692. X                break;
  693. X    
  694. X            case AUTH_REJECTEDCRED:
  695. X                fprintf(stderr, "AUTH_REJECTED_CREDENTIAL");
  696. X                break;
  697. X    
  698. X            case AUTH_BADVERF:
  699. X                fprintf(stderr, "AUTH_BOGUS_VERIFIER");
  700. X                break;
  701. X    
  702. X            case AUTH_REJECTEDVERF:
  703. X                fprintf(stderr, "AUTH_REJECTED_VERIFIER");
  704. X                break;
  705. X    
  706. X            case AUTH_TOOWEAK:
  707. X                fprintf(stderr, "AUTH_TOO_WEAK (remote error)");
  708. X                break;
  709. X    
  710. X            case AUTH_INVALIDRESP:
  711. X                fprintf(stderr, "AUTH_INVALID_RESPONSE");
  712. X                break;
  713. X    
  714. X            default:
  715. X                fprintf(stderr, "AUTH_UNKNOWN_FAILURE");
  716. X                break;
  717. X            }
  718. X            break;
  719. X    
  720. X        case RPC_PROGVERSMISMATCH:
  721. X            clnt_perrno(e.re_status);
  722. X            fprintf(stderr, "; low version = %lu, high version = %lu", e.re_vers.low, e.re_vers.high);
  723. X            break;
  724. X    
  725. X        default:
  726. X            fprintf(stderr, "RPC_UNKNOWN_FAILURE; s1 = %lu, s2 = %lu", e.re_lb.s1, e.re_lb.s2);
  727. X            break;
  728. X    }
  729. X    fprintf(stderr, "\n");
  730. X}
  731. X
  732. X/*
  733. X * This interface for use by clntrpc
  734. X */
  735. Xvoid
  736. Xclnt_perrno(num)
  737. X    enum clnt_stat num;
  738. X{
  739. X    switch (num) {
  740. X        case RPC_SUCCESS:
  741. X            fprintf(stderr, "RPC_SUCCESS");
  742. X            break;
  743. X    
  744. X        case RPC_CANTENCODEARGS:
  745. X            fprintf(stderr, "RPC_CANT_ENCODE_ARGS");
  746. X            break;
  747. X    
  748. X        case RPC_CANTDECODERES:
  749. X            fprintf(stderr, "RPC_CANT_DECODE_RESULTS");
  750. X            break;
  751. X    
  752. X        case RPC_CANTSEND:
  753. X            fprintf(stderr, "RPC_CANT_SEND");
  754. X            break;
  755. X    
  756. X        case RPC_CANTRECV:
  757. X            fprintf(stderr, "RPC_CANT_RECV");
  758. X            break;
  759. X    
  760. X        case RPC_TIMEDOUT:
  761. X            fprintf(stderr, "RPC_TIMED_OUT");
  762. X            break;
  763. X    
  764. X        case RPC_VERSMISMATCH:
  765. X            fprintf(stderr, "RPC_VERSION_MISMATCH");
  766. X            break;
  767. X    
  768. X        case RPC_AUTHERROR:
  769. X            fprintf(stderr, "RPC_AUTH_ERROR");
  770. X            break;
  771. X    
  772. X        case RPC_PROGUNAVAIL:
  773. X            fprintf(stderr, "RPC_REMOTE_PROGRAM_UNAVAILABLE");
  774. X            break;
  775. X    
  776. X        case RPC_PROGVERSMISMATCH:
  777. X            fprintf(stderr, "RPC_PROGRAM_MISMATCH");
  778. X            break;
  779. X    
  780. X        case RPC_PROCUNAVAIL:
  781. X            fprintf(stderr, "RPC_UNKNOWN_PROCEDURE");
  782. X            break;
  783. X    
  784. X        case RPC_CANTDECODEARGS:
  785. X            fprintf(stderr, "RPC_CANT_DECODE_ARGS");
  786. X            break;
  787. X        case RPC_UNKNOWNHOST:
  788. X            fprintf(stderr, "RPC_UNKNOWNHOST");
  789. X            break;
  790. X        case RPC_PMAPFAILURE:
  791. X            fprintf(stderr, "RPC_PMAP_FAILURE");
  792. X            break;
  793. X        case RPC_PROGNOTREGISTERED:
  794. X            fprintf(stderr, "RPC_PROG_NOT_REGISTERED");
  795. X            break;
  796. X        case RPC_SYSTEMERROR:
  797. X            fprintf(stderr, "RPC_SYSTEM_ERROR");
  798. X            break;
  799. X    }
  800. X}
  801. X
  802. X/*
  803. X * A handle on why an rpc creation routine failed (returned NULL.)
  804. X */
  805. Xstruct rpc_createerr rpc_createerr;
  806. X
  807. Xclnt_pcreateerror(s)
  808. X    char *s;
  809. X{
  810. X
  811. X    fprintf(stderr, "%s: ", s);
  812. X    clnt_perrno(rpc_createerr.cf_stat);
  813. X    switch (rpc_createerr.cf_stat) {
  814. X        case RPC_PMAPFAILURE:
  815. X            fprintf(stderr, " - ");
  816. X            clnt_perrno(rpc_createerr.cf_error.re_status);
  817. X            break;
  818. X
  819. X        case RPC_SYSTEMERROR:
  820. X            fprintf(stderr, " - %s", sys_errlist[rpc_createerr.cf_error.re_errno]);
  821. X            break;
  822. X
  823. X    }
  824. X    fprintf(stderr, "\n");
  825. X}
  826. SHAR_EOF
  827. if test 5331 -ne "`wc -c < 'rpc/rpclib/clnt_perror.c'`"
  828. then
  829.     echo shar: "error transmitting 'rpc/rpclib/clnt_perror.c'" '(should have been 5331 characters)'
  830. fi
  831. chmod 444 'rpc/rpclib/clnt_perror.c'
  832. fi
  833. echo shar: "extracting 'rpc/rpclib/clnt_raw.c'" '(5285 characters)'
  834. if test -f 'rpc/rpclib/clnt_raw.c'
  835. then
  836.     echo shar: "will not over-write existing file 'rpc/rpclib/clnt_raw.c'"
  837. else
  838. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/clnt_raw.c'
  839. X/*
  840. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  841. X * unrestricted use provided that this legend is included on all tape
  842. X * media and as a part of the software program in whole or part.  Users
  843. X * may copy or modify Sun RPC without charge, but are not authorized
  844. X * to license or distribute it to anyone else except as part of a product or
  845. X * program developed by the user.
  846. X * 
  847. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  848. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  849. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  850. X * 
  851. X * Sun RPC is provided with no support and without any obligation on the
  852. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  853. X * modification or enhancement.
  854. X * 
  855. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  856. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  857. X * OR ANY PART THEREOF.
  858. X * 
  859. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  860. X * or profits or other special, indirect and consequential damages, even if
  861. X * Sun has been advised of the possibility of such damages.
  862. X * 
  863. X * Sun Microsystems, Inc.
  864. X * 2550 Garcia Avenue
  865. X * Mountain View, California  94043
  866. X */
  867. X#ifndef lint
  868. Xstatic char sccsid[] = "@(#)clnt_raw.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  869. X#endif
  870. X
  871. X/*
  872. X * clnt_raw.c
  873. X *
  874. X * Copyright (C) 1984, Sun Microsystems, Inc.
  875. X *
  876. X * Memory based rpc for simple testing and timing.
  877. X * Interface to create an rpc client and server in the same process.
  878. X * This lets us similate rpc and get round trip overhead, without
  879. X * any interference from the kernal.
  880. X */
  881. X
  882. X#include "types.h"
  883. X#include <sys/time.h>
  884. X#include <netinet/in.h>
  885. X#include "xdr.h"
  886. X#include "auth.h"
  887. X#include "clnt.h"
  888. X#include "rpc_msg.h"
  889. X
  890. X#define NULL ((caddr_t)0)
  891. X#define MCALL_MSG_SIZE 24
  892. X
  893. X/*
  894. X * This is the "network" we will be moving stuff over.
  895. X */
  896. Xchar _raw_buf[UDPMSGSIZE];
  897. X
  898. Xstatic char    mashl_callmsg[MCALL_MSG_SIZE];
  899. Xstatic u_int    mcnt;
  900. X
  901. Xstatic enum clnt_stat    clntraw_call();
  902. Xstatic void        clntraw_abort();
  903. Xstatic void        clntraw_geterr();
  904. Xstatic bool_t        clntraw_freeres();
  905. Xstatic void        clntraw_destroy();
  906. X
  907. Xstatic struct clnt_ops client_ops = {
  908. X    clntraw_call,
  909. X    clntraw_abort,
  910. X    clntraw_geterr,
  911. X    clntraw_freeres,
  912. X    clntraw_destroy
  913. X};
  914. X
  915. Xstatic CLIENT    client_object;
  916. Xstatic XDR    xdr_stream;
  917. X
  918. Xvoid    svc_getreq();
  919. X
  920. X/*
  921. X * Create a client handle for memory based rpc.
  922. X */
  923. XCLIENT *
  924. Xclntraw_create(prog, vers)
  925. X    u_long prog;
  926. X    u_long vers;
  927. X{
  928. X    struct rpc_msg call_msg;
  929. X    XDR *xdrs = &xdr_stream;
  930. X    CLIENT    *client = &client_object;
  931. X
  932. X    /*
  933. X     * pre-serialize the staic part of the call msg and stash it away
  934. X     */
  935. X    call_msg.rm_direction = CALL;
  936. X    call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  937. X    call_msg.rm_call.cb_prog = prog;
  938. X    call_msg.rm_call.cb_vers = vers;
  939. X    xdrmem_create(xdrs, mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 
  940. X    if (! xdr_callhdr(xdrs, &call_msg)) {
  941. X        perror("clnt_raw.c - Fatal header serialization error.");
  942. X    }
  943. X    mcnt = XDR_GETPOS(xdrs);
  944. X    XDR_DESTROY(xdrs);
  945. X
  946. X    /*
  947. X     * Set xdrmem for client/server shared buffer
  948. X     */
  949. X    xdrmem_create(xdrs, _raw_buf, UDPMSGSIZE, XDR_FREE);
  950. X
  951. X    /*
  952. X     * create client handle
  953. X     */
  954. X    client->cl_ops = &client_ops;
  955. X    client->cl_auth = authnone_create();
  956. X    return (client);
  957. X}
  958. X
  959. Xstatic enum clnt_stat 
  960. Xclntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
  961. X    CLIENT *h;
  962. X    u_long proc;
  963. X    xdrproc_t xargs;
  964. X    caddr_t argsp;
  965. X    xdrproc_t xresults;
  966. X    caddr_t resultsp;
  967. X    struct timeval timeout;
  968. X{
  969. X    register XDR *xdrs = &xdr_stream;
  970. X    struct rpc_msg msg;
  971. X    enum clnt_stat status;
  972. X    struct rpc_err error;
  973. X
  974. Xcall_again:
  975. X    /*
  976. X     * send request
  977. X     */
  978. X    xdrs->x_op = XDR_ENCODE;
  979. X    XDR_SETPOS(xdrs, 0);
  980. X    ((struct rpc_msg *)mashl_callmsg)->rm_xid ++ ;
  981. X    if ((! XDR_PUTBYTES(xdrs, mashl_callmsg, mcnt)) ||
  982. X        (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  983. X        (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  984. X        (! (*xargs)(xdrs, argsp))) {
  985. X        return (RPC_CANTENCODEARGS);
  986. X    }
  987. X    (void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
  988. X
  989. X    /*
  990. X     * We have to call server input routine here because this is
  991. X     * all going on in one process. Yuk.
  992. X     */
  993. X    svc_getreq(1);
  994. X
  995. X    /*
  996. X     * get results
  997. X     */
  998. X    xdrs->x_op = XDR_DECODE;
  999. X    XDR_SETPOS(xdrs, 0);
  1000. X    msg.acpted_rply.ar_verf = _null_auth;
  1001. X    msg.acpted_rply.ar_results.where = resultsp;
  1002. X    msg.acpted_rply.ar_results.proc = xresults;
  1003. X    if (! xdr_replymsg(xdrs, &msg))
  1004. X        return (RPC_CANTDECODERES);
  1005. X    _seterr_reply(&msg, &error);
  1006. X    status = error.re_status;
  1007. X
  1008. X    if (status == RPC_SUCCESS) {
  1009. X        if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
  1010. X            status = RPC_AUTHERROR;
  1011. X        }
  1012. X    }  /* end successful completion */
  1013. X    else {
  1014. X        if (AUTH_REFRESH(h->cl_auth))
  1015. X            goto call_again;
  1016. X    }  /* end of unsuccessful completion */
  1017. X
  1018. X    if (status == RPC_SUCCESS) {
  1019. X        if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
  1020. X            status = RPC_AUTHERROR;
  1021. X        }
  1022. X        if (msg.acpted_rply.ar_verf.oa_base != NULL) {
  1023. X            xdrs->x_op = XDR_FREE;
  1024. X            (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
  1025. X        }
  1026. X    }
  1027. X
  1028. X    return (status);
  1029. X}
  1030. X
  1031. Xstatic void
  1032. Xclntraw_geterr()
  1033. X{
  1034. X}
  1035. X
  1036. X
  1037. Xstatic bool_t
  1038. Xclntraw_freeres(cl, xdr_res, res_ptr)
  1039. X    CLIENT *cl;
  1040. X    xdrproc_t xdr_res;
  1041. X    caddr_t res_ptr;
  1042. X{
  1043. X    register XDR *xdrs = &xdr_stream;
  1044. X
  1045. X    xdrs->x_op = XDR_FREE;
  1046. X    return ((*xdr_res)(xdrs, res_ptr));
  1047. X}
  1048. X
  1049. Xstatic void
  1050. Xclntraw_abort()
  1051. X{
  1052. X}
  1053. X
  1054. Xstatic void
  1055. Xclntraw_destroy()
  1056. X{
  1057. X}
  1058. SHAR_EOF
  1059. if test 5285 -ne "`wc -c < 'rpc/rpclib/clnt_raw.c'`"
  1060. then
  1061.     echo shar: "error transmitting 'rpc/rpclib/clnt_raw.c'" '(should have been 5285 characters)'
  1062. fi
  1063. chmod 444 'rpc/rpclib/clnt_raw.c'
  1064. fi
  1065. echo shar: "extracting 'rpc/rpclib/clnt_simple.c'" '(3016 characters)'
  1066. if test -f 'rpc/rpclib/clnt_simple.c'
  1067. then
  1068.     echo shar: "will not over-write existing file 'rpc/rpclib/clnt_simple.c'"
  1069. else
  1070. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/clnt_simple.c'
  1071. X/*
  1072. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1073. X * unrestricted use provided that this legend is included on all tape
  1074. X * media and as a part of the software program in whole or part.  Users
  1075. X * may copy or modify Sun RPC without charge, but are not authorized
  1076. X * to license or distribute it to anyone else except as part of a product or
  1077. X * program developed by the user.
  1078. X * 
  1079. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1080. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1081. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1082. X * 
  1083. X * Sun RPC is provided with no support and without any obligation on the
  1084. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1085. X * modification or enhancement.
  1086. X * 
  1087. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1088. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1089. X * OR ANY PART THEREOF.
  1090. X * 
  1091. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1092. X * or profits or other special, indirect and consequential damages, even if
  1093. X * Sun has been advised of the possibility of such damages.
  1094. X * 
  1095. X * Sun Microsystems, Inc.
  1096. X * 2550 Garcia Avenue
  1097. X * Mountain View, California  94043
  1098. X */
  1099. X#ifndef lint
  1100. Xstatic char sccsid[] = "@(#)clnt_simple.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1101. X#endif
  1102. X
  1103. X/* 
  1104. X * clnt_simple.c
  1105. X * Simplified front end to rpc.
  1106. X *
  1107. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1108. X */
  1109. X
  1110. X#include <stdio.h>
  1111. X#include <rpc/rpc.h>
  1112. X#include <sys/socket.h>
  1113. X#include <sys/time.h>
  1114. X#include <netdb.h>
  1115. X
  1116. Xchar *malloc();
  1117. Xstatic CLIENT *client;
  1118. Xstatic int socket;
  1119. Xstatic int oldprognum, oldversnum, valid;
  1120. Xstatic char *oldhost;
  1121. X
  1122. Xcallrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
  1123. X    char *host;
  1124. X    xdrproc_t inproc, outproc;
  1125. X    char *in, *out;
  1126. X{
  1127. X    struct sockaddr_in server_addr;
  1128. X    enum clnt_stat clnt_stat;
  1129. X    struct hostent *hp;
  1130. X    struct timeval timeout, tottimeout;
  1131. X
  1132. X    if (oldhost == NULL) {
  1133. X        oldhost = malloc(256);
  1134. X        oldhost[0] = 0;
  1135. X        socket = RPC_ANYSOCK;
  1136. X    }
  1137. X    if (valid && oldprognum == prognum && oldversnum == versnum
  1138. X        && strcmp(oldhost, host) == 0) {
  1139. X        /* reuse old client */        
  1140. X    }
  1141. X    else {
  1142. X        valid = 0;
  1143. X        close(socket);
  1144. X        socket = RPC_ANYSOCK;
  1145. X        if (client) {
  1146. X            clnt_destroy(client);
  1147. X            client = NULL;
  1148. X        }
  1149. X        if ((hp = gethostbyname(host)) == NULL)
  1150. X            return ((int) RPC_UNKNOWNHOST);
  1151. X        timeout.tv_usec = 0;
  1152. X        timeout.tv_sec = 5;
  1153. X        bcopy(hp->h_addr, &server_addr.sin_addr, hp->h_length);
  1154. X        server_addr.sin_family = AF_INET;
  1155. X        server_addr.sin_port =  0;
  1156. X        if ((client = clntudp_create(&server_addr, prognum,
  1157. X            versnum, timeout, &socket)) == NULL)
  1158. X            return ((int) rpc_createerr.cf_stat);
  1159. X        valid = 1;
  1160. X        oldprognum = prognum;
  1161. X        oldversnum = versnum;
  1162. X        strcpy(oldhost, host);
  1163. X    }
  1164. X    tottimeout.tv_sec = 25;
  1165. X    tottimeout.tv_usec = 0;
  1166. X    clnt_stat = clnt_call(client, procnum, inproc, in,
  1167. X        outproc, out, tottimeout);
  1168. X    /* 
  1169. X     * if call failed, empty cache
  1170. X     */
  1171. X    if (clnt_stat != RPC_SUCCESS)
  1172. X        valid = 0;
  1173. X    return ((int) clnt_stat);
  1174. X}
  1175. SHAR_EOF
  1176. if test 3016 -ne "`wc -c < 'rpc/rpclib/clnt_simple.c'`"
  1177. then
  1178.     echo shar: "error transmitting 'rpc/rpclib/clnt_simple.c'" '(should have been 3016 characters)'
  1179. fi
  1180. chmod 444 'rpc/rpclib/clnt_simple.c'
  1181. fi
  1182. echo shar: "extracting 'rpc/rpclib/clnt_tcp.c'" '(10804 characters)'
  1183. if test -f 'rpc/rpclib/clnt_tcp.c'
  1184. then
  1185.     echo shar: "will not over-write existing file 'rpc/rpclib/clnt_tcp.c'"
  1186. else
  1187. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/clnt_tcp.c'
  1188. X/*
  1189. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1190. X * unrestricted use provided that this legend is included on all tape
  1191. X * media and as a part of the software program in whole or part.  Users
  1192. X * may copy or modify Sun RPC without charge, but are not authorized
  1193. X * to license or distribute it to anyone else except as part of a product or
  1194. X * program developed by the user.
  1195. X * 
  1196. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1197. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1198. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1199. X * 
  1200. X * Sun RPC is provided with no support and without any obligation on the
  1201. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1202. X * modification or enhancement.
  1203. X * 
  1204. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1205. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1206. X * OR ANY PART THEREOF.
  1207. X * 
  1208. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1209. X * or profits or other special, indirect and consequential damages, even if
  1210. X * Sun has been advised of the possibility of such damages.
  1211. X * 
  1212. X * Sun Microsystems, Inc.
  1213. X * 2550 Garcia Avenue
  1214. X * Mountain View, California  94043
  1215. X */
  1216. X#ifndef lint
  1217. Xstatic char sccsid[] = "@(#)clnt_tcp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1218. X#endif
  1219. X/*
  1220. X * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
  1221. X *
  1222. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1223. X *
  1224. X * TCP based RPC supports 'batched calls'.
  1225. X * A sequence of calls may be batched-up in a send buffer.  The rpc call
  1226. X * return immediately to the client even though the call was not necessarily
  1227. X * sent.  The batching occurs iff the results' xdr routine is NULL (0) AND
  1228. X * the rpc timeout value is zero (see clnt.h, rpc).
  1229. X *
  1230. X * Clients should NOT casually batch calls that in fact return results; that is,
  1231. X * the server side should be aware that a call is batched and not produce any
  1232. X * return message.  Batched calls that produce many result messages can
  1233. X * deadlock (netlock) the client and the server....
  1234. X *
  1235. X * Now go hang yourself.
  1236. X */
  1237. X
  1238. X#include <stdio.h>
  1239. X#include "types.h"
  1240. X#include <sys/socket.h>
  1241. X#include <sys/time.h>
  1242. X#include <netinet/in.h>
  1243. X#include <netdb.h>
  1244. X#include <errno.h>
  1245. X#include "xdr.h"
  1246. X#include "auth.h"
  1247. X#include "clnt.h"
  1248. X#include "rpc_msg.h"
  1249. X#include "pmap_clnt.h"
  1250. X
  1251. X#define MCALL_MSG_SIZE 24
  1252. X
  1253. Xchar *malloc();
  1254. Xextern int errno;
  1255. X
  1256. Xstatic int    readtcp();
  1257. Xstatic int    writetcp();
  1258. X
  1259. Xstatic enum clnt_stat    clnttcp_call();
  1260. Xstatic void        clnttcp_abort();
  1261. Xstatic void        clnttcp_geterr();
  1262. Xstatic bool_t        clnttcp_freeres();
  1263. Xstatic void        clnttcp_destroy();
  1264. X
  1265. Xstatic struct clnt_ops tcp_ops = {
  1266. X    clnttcp_call,
  1267. X    clnttcp_abort,
  1268. X    clnttcp_geterr,
  1269. X    clnttcp_freeres,
  1270. X    clnttcp_destroy
  1271. X};
  1272. X
  1273. Xstruct ct_data {
  1274. X    int        ct_sock;
  1275. X    struct timeval    ct_wait;
  1276. X    struct rpc_err    ct_error;
  1277. X    char        ct_mcall[MCALL_MSG_SIZE];    /* marshalled callmsg */
  1278. X    u_int        ct_mpos;            /* pos after marshal */
  1279. X    XDR        ct_xdrs;
  1280. X};
  1281. X
  1282. X/*
  1283. X * Create a client handle for a tcp/ip connection.
  1284. X * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  1285. X * connected to raddr.  If *sockp non-negative then
  1286. X * raddr is ignored.  The rpc/tcp package does buffering
  1287. X * similar to stdio, so the client must pick send and receive buffer sizes,];
  1288. X * 0 => use the default.
  1289. X * If raddr->sin_port is 0, then a binder on the remote machine is
  1290. X * consulted for the right port number.
  1291. X * NB: *sockp is copied into a private area.
  1292. X * NB: It is the clients responsibility to close *sockp.
  1293. X * NB: The rpch->cl_auth is set null authentication.  Caller may wish to set this
  1294. X * something more useful.
  1295. X */
  1296. XCLIENT *
  1297. Xclnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
  1298. X    struct sockaddr_in *raddr;
  1299. X    u_long prog;
  1300. X    u_long vers;
  1301. X    register int *sockp;
  1302. X    u_int sendsz;
  1303. X    u_int recvsz;
  1304. X{
  1305. X    CLIENT *h;
  1306. X    register struct ct_data *ct;
  1307. X    struct timeval now;
  1308. X    struct rpc_msg call_msg;
  1309. X
  1310. X    h  = (CLIENT *)mem_alloc(sizeof(*h));
  1311. X    if (h == NULL) {
  1312. X        fprintf(stderr, "clnttcp_create: out of memory\n");
  1313. X        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1314. X        rpc_createerr.cf_error.re_errno = errno;
  1315. X        goto fooy;
  1316. X    }
  1317. X    ct = (struct ct_data *)mem_alloc(sizeof(*ct));
  1318. X    if (ct == NULL) {
  1319. X        fprintf(stderr, "clnttcp_create: out of memory\n");
  1320. X        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1321. X        rpc_createerr.cf_error.re_errno = errno;
  1322. X        goto fooy;
  1323. X    }
  1324. X
  1325. X    /*
  1326. X     * If no port number given ask the pmap for one
  1327. X     */
  1328. X    if (raddr->sin_port == 0) {
  1329. X        u_short port;
  1330. X        if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
  1331. X            mem_free((caddr_t)ct, sizeof(struct ct_data));
  1332. X            mem_free((caddr_t)h, sizeof(CLIENT));
  1333. X            return ((CLIENT *)NULL);
  1334. X        }
  1335. X        raddr->sin_port = htons(port);
  1336. X    }
  1337. X
  1338. X    /*
  1339. X     * If no socket given, open one
  1340. X     */
  1341. X    if (*sockp < 0) {
  1342. X        if (((*sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  1343. X            || (connect(*sockp, (struct sockaddr *)raddr,
  1344. X            sizeof(*raddr)) < 0)) {
  1345. X            rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1346. X            rpc_createerr.cf_error.re_errno = errno;
  1347. X            goto fooy;
  1348. X        }
  1349. X    }
  1350. X
  1351. X    /*
  1352. X     * Set up private data struct
  1353. X     */
  1354. X    ct->ct_sock = *sockp;
  1355. X    ct->ct_wait.tv_usec = 0;
  1356. X
  1357. X    /*
  1358. X     * Initialize call message
  1359. X     */
  1360. X    (void)gettimeofday(&now, (struct timezone *)0);
  1361. X    call_msg.rm_xid = getpid() ^  now.tv_sec ^ now.tv_usec;
  1362. X    call_msg.rm_direction = CALL;
  1363. X    call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  1364. X    call_msg.rm_call.cb_prog = prog;
  1365. X    call_msg.rm_call.cb_vers = vers;
  1366. X
  1367. X    /*
  1368. X     * pre-serialize the staic part of the call msg and stash it away
  1369. X     */
  1370. X    xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
  1371. X        XDR_ENCODE);
  1372. X    if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
  1373. X        goto fooy;
  1374. X    }
  1375. X    ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
  1376. X    XDR_DESTROY(&(ct->ct_xdrs));
  1377. X
  1378. X    /*
  1379. X     * Create a client handle which uses xdrrec for serialization
  1380. X     * and authnone for authentication.
  1381. X     */
  1382. X    xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
  1383. X        (caddr_t)ct, readtcp, writetcp);
  1384. X    h->cl_ops = &tcp_ops;
  1385. X    h->cl_private = (caddr_t) ct;
  1386. X    h->cl_auth = authnone_create();
  1387. X    return (h);
  1388. X
  1389. Xfooy:
  1390. X    /*
  1391. X     * Something goofed, free stuff and barf
  1392. X     */
  1393. X    mem_free((caddr_t)ct, sizeof(struct ct_data));
  1394. X    mem_free((caddr_t)h, sizeof(CLIENT));
  1395. X    (void)close(*sockp);
  1396. X    return ((CLIENT *)NULL);
  1397. X}
  1398. X
  1399. Xstatic enum clnt_stat
  1400. Xclnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
  1401. X    register CLIENT *h;
  1402. X    u_long proc;
  1403. X    xdrproc_t xdr_args;
  1404. X    caddr_t args_ptr;
  1405. X    xdrproc_t xdr_results;
  1406. X    caddr_t results_ptr;
  1407. X    struct timeval timeout;
  1408. X{
  1409. X    register struct ct_data *ct = (struct ct_data *) h->cl_private;
  1410. X    register XDR *xdrs = &(ct->ct_xdrs);
  1411. X    struct rpc_msg reply_msg;
  1412. X    u_long x_id;
  1413. X    u_long *msg_x_id = (u_long *)(ct->ct_mcall);    /* yuk */
  1414. X    register bool_t shipnow;
  1415. X
  1416. X    ct->ct_wait = timeout;
  1417. X    shipnow =
  1418. X        (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
  1419. X        && timeout.tv_usec == 0) ? FALSE : TRUE;
  1420. X
  1421. Xcall_again:
  1422. X    xdrs->x_op = XDR_ENCODE;
  1423. X    ct->ct_error.re_status = RPC_SUCCESS;
  1424. X    x_id = ntohl(--(*msg_x_id));
  1425. X    if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  1426. X        (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  1427. X        (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  1428. X        (! (*xdr_args)(xdrs, args_ptr))) {
  1429. X        if (ct->ct_error.re_status == RPC_SUCCESS)
  1430. X            ct->ct_error.re_status = RPC_CANTENCODEARGS;
  1431. X        (void)xdrrec_endofrecord(xdrs, TRUE);
  1432. X        return (ct->ct_error.re_status);
  1433. X    }
  1434. X    if (! xdrrec_endofrecord(xdrs, shipnow))
  1435. X        return (ct->ct_error.re_status = RPC_CANTSEND);
  1436. X    if (! shipnow)
  1437. X        return (RPC_SUCCESS);
  1438. X    xdrs->x_op = XDR_DECODE;
  1439. X
  1440. X    /*
  1441. X     * Keep receiving until we get a valid transaction id
  1442. X     */
  1443. X    while (TRUE) {
  1444. X        reply_msg.acpted_rply.ar_verf = _null_auth;
  1445. X        reply_msg.acpted_rply.ar_results.where = NULL;
  1446. X        reply_msg.acpted_rply.ar_results.proc = xdr_void;
  1447. X        if (! xdrrec_skiprecord(xdrs))
  1448. X            return (ct->ct_error.re_status);
  1449. X        /* now decode and validate the response header */
  1450. X        if (! xdr_replymsg(xdrs, &reply_msg)) {
  1451. X            if (ct->ct_error.re_status == RPC_SUCCESS)
  1452. X                continue;
  1453. X            return (ct->ct_error.re_status);
  1454. X        }
  1455. X        if (reply_msg.rm_xid == x_id)
  1456. X            break;
  1457. X    }
  1458. X
  1459. X    /*
  1460. X     * process header
  1461. X     */
  1462. X    _seterr_reply(&reply_msg, &(ct->ct_error));
  1463. X    if (ct->ct_error.re_status == RPC_SUCCESS) {
  1464. X        if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
  1465. X            ct->ct_error.re_status = RPC_AUTHERROR;
  1466. X            ct->ct_error.re_why = AUTH_INVALIDRESP;
  1467. X        } else if (! (*xdr_results)(xdrs, results_ptr)) {
  1468. X            if (ct->ct_error.re_status == RPC_SUCCESS)
  1469. X                ct->ct_error.re_status = RPC_CANTDECODERES;
  1470. X        }
  1471. X        /* free verifier ... */
  1472. X        if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  1473. X            xdrs->x_op = XDR_FREE;
  1474. X            (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  1475. X        }
  1476. X    }  /* end successful completion */
  1477. X    else {
  1478. X        /* maybe our credentials need to be refreshed ... */
  1479. X        if (AUTH_REFRESH(h->cl_auth))
  1480. X            goto call_again;
  1481. X    }  /* end of unsuccessful completion */
  1482. X    return (ct->ct_error.re_status);
  1483. X}
  1484. X
  1485. Xstatic void
  1486. Xclnttcp_geterr(h, errp)
  1487. X    CLIENT *h;
  1488. X    struct rpc_err *errp;
  1489. X{
  1490. X    register struct ct_data *ct =
  1491. X        (struct ct_data *) h->cl_private;
  1492. X
  1493. X    *errp = ct->ct_error;
  1494. X}
  1495. X
  1496. Xstatic bool_t
  1497. Xclnttcp_freeres(cl, xdr_res, res_ptr)
  1498. X    CLIENT *cl;
  1499. X    xdrproc_t xdr_res;
  1500. X    caddr_t res_ptr;
  1501. X{
  1502. X    register struct ct_data *ct = (struct ct_data *)cl->cl_private;
  1503. X    register XDR *xdrs = &(ct->ct_xdrs);
  1504. X
  1505. X    xdrs->x_op = XDR_FREE;
  1506. X    return ((*xdr_res)(xdrs, res_ptr));
  1507. X}
  1508. X
  1509. Xstatic void
  1510. Xclnttcp_abort()
  1511. X{
  1512. X}
  1513. X
  1514. Xstatic void
  1515. Xclnttcp_destroy(h)
  1516. X    CLIENT *h;
  1517. X{
  1518. X    register struct ct_data *ct =
  1519. X        (struct ct_data *) h->cl_private;
  1520. X
  1521. X    XDR_DESTROY(&(ct->ct_xdrs));
  1522. X    mem_free((caddr_t)ct, sizeof(struct ct_data));
  1523. X    mem_free((caddr_t)h, sizeof(CLIENT));
  1524. X}
  1525. X
  1526. X/*
  1527. X * Interface between xdr serializer and tcp connection.
  1528. X * Behaves like the system calls, read & write, but keeps some error state
  1529. X * around for the rpc level.
  1530. X */
  1531. Xstatic int
  1532. Xreadtcp(ct, buf, len)
  1533. X    register struct ct_data *ct;
  1534. X    caddr_t buf;
  1535. X    register int len;
  1536. X{
  1537. X    register int mask = 1 << (ct->ct_sock);
  1538. X    int readfds;
  1539. X
  1540. X    if (len == 0)
  1541. X        return (0);
  1542. X    while (TRUE) {
  1543. X        readfds = mask;
  1544. X        switch (select(32, &readfds, (int*)NULL, (int*)NULL,
  1545. X            &(ct->ct_wait))) {
  1546. X
  1547. X        case 0:
  1548. X            ct->ct_error.re_status = RPC_TIMEDOUT;
  1549. X            return (-1);
  1550. X
  1551. X        case -1:
  1552. X            if (errno == EINTR)
  1553. X                continue;
  1554. X            ct->ct_error.re_status = RPC_CANTRECV;
  1555. X            ct->ct_error.re_errno = errno;
  1556. X            return (-1);
  1557. X        }
  1558. X        if (readfds == mask)
  1559. X            break;
  1560. X    }
  1561. X    switch (len = read(ct->ct_sock, buf, len)) {
  1562. X
  1563. X    case 0:
  1564. X        /* premature eof */
  1565. X        ct->ct_error.re_errno = ECONNRESET;
  1566. X        ct->ct_error.re_status = RPC_CANTRECV;
  1567. X        len = -1;  /* it's really an error */
  1568. X        break;
  1569. X
  1570. X    case -1:
  1571. X        ct->ct_error.re_errno = errno;
  1572. X        ct->ct_error.re_status = RPC_CANTRECV;
  1573. X        break;
  1574. X    }
  1575. X    return (len);
  1576. X}
  1577. X
  1578. Xstatic int
  1579. Xwritetcp(ct, buf, len)
  1580. X    struct ct_data *ct;
  1581. X    caddr_t buf;
  1582. X    int len;
  1583. X{
  1584. X    register int i, cnt;
  1585. X
  1586. X    for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  1587. X        if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
  1588. X            ct->ct_error.re_errno = errno;
  1589. X            ct->ct_error.re_status = RPC_CANTSEND;
  1590. X            return (-1);
  1591. X        }
  1592. X    }
  1593. X    return (len);
  1594. X}
  1595. SHAR_EOF
  1596. if test 10804 -ne "`wc -c < 'rpc/rpclib/clnt_tcp.c'`"
  1597. then
  1598.     echo shar: "error transmitting 'rpc/rpclib/clnt_tcp.c'" '(should have been 10804 characters)'
  1599. fi
  1600. chmod 444 'rpc/rpclib/clnt_tcp.c'
  1601. fi
  1602. echo shar: "extracting 'rpc/rpclib/clnt_udp.c'" '(10438 characters)'
  1603. if test -f 'rpc/rpclib/clnt_udp.c'
  1604. then
  1605.     echo shar: "will not over-write existing file 'rpc/rpclib/clnt_udp.c'"
  1606. else
  1607. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/clnt_udp.c'
  1608. X/*
  1609. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1610. X * unrestricted use provided that this legend is included on all tape
  1611. X * media and as a part of the software program in whole or part.  Users
  1612. X * may copy or modify Sun RPC without charge, but are not authorized
  1613. X * to license or distribute it to anyone else except as part of a product or
  1614. X * program developed by the user.
  1615. X * 
  1616. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1617. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1618. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1619. X * 
  1620. X * Sun RPC is provided with no support and without any obligation on the
  1621. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1622. X * modification or enhancement.
  1623. X * 
  1624. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1625. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1626. X * OR ANY PART THEREOF.
  1627. X * 
  1628. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1629. X * or profits or other special, indirect and consequential damages, even if
  1630. X * Sun has been advised of the possibility of such damages.
  1631. X * 
  1632. X * Sun Microsystems, Inc.
  1633. X * 2550 Garcia Avenue
  1634. X * Mountain View, California  94043
  1635. X */
  1636. X#ifndef lint
  1637. Xstatic char sccsid[] = "@(#)clnt_udp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1638. X#endif
  1639. X
  1640. X/*
  1641. X * clnt_udp.c, Implements a UPD/IP based, client side RPC.
  1642. X *
  1643. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1644. X */
  1645. X
  1646. X#include <stdio.h>
  1647. X#include "types.h"
  1648. X#include <sys/socket.h>
  1649. X#include <sys/time.h>
  1650. X#include <sys/ioctl.h>
  1651. X#include <netinet/in.h>
  1652. X#include <netdb.h>
  1653. X#include <errno.h>
  1654. X#include "xdr.h"
  1655. X#include "auth.h"
  1656. X#include "clnt.h"
  1657. X#include "rpc_msg.h"
  1658. X#include "pmap_clnt.h"
  1659. X
  1660. Xchar *malloc();
  1661. Xextern int errno;
  1662. X
  1663. X/*
  1664. X * UDP bases client side rpc operations
  1665. X */
  1666. Xstatic enum clnt_stat    clntudp_call();
  1667. Xstatic void        clntudp_abort();
  1668. Xstatic void        clntudp_geterr();
  1669. Xstatic bool_t        clntudp_freeres();
  1670. Xstatic void        clntudp_destroy();
  1671. X
  1672. Xstatic struct clnt_ops udp_ops = {
  1673. X    clntudp_call,
  1674. X    clntudp_abort,
  1675. X    clntudp_geterr,
  1676. X    clntudp_freeres,
  1677. X    clntudp_destroy
  1678. X};
  1679. X
  1680. X/* 
  1681. X * Private data kept per client handle
  1682. X */
  1683. Xstruct cu_data {
  1684. X    int           cu_sock;
  1685. X    struct sockaddr_in cu_raddr;
  1686. X    int           cu_rlen;
  1687. X    struct timeval       cu_wait;
  1688. X    struct rpc_err       cu_error;
  1689. X    XDR           cu_outxdrs;
  1690. X    u_int           cu_xdrpos;
  1691. X    u_int           cu_sendsz;
  1692. X    char           *cu_outbuf;
  1693. X    u_int           cu_recvsz;
  1694. X    char           cu_inbuf[1];
  1695. X};
  1696. X
  1697. X/*
  1698. X * Create a UDP based client handle.
  1699. X * If *sockp<0, *sockp is set to a newly created UPD socket.
  1700. X * If raddr->sin_port is 0 a binder on the remote machine
  1701. X * is consulted for the correct port number.
  1702. X * NB: It is the clients responsibility to close *sockp.
  1703. X * NB: The rpch->cl_auth is initialized to null authentication.
  1704. X *     Caller may wish to set this something more useful.
  1705. X *
  1706. X * wait is the amount of time used between retransmitting a call if
  1707. X * no response has been heard;  retransmition occurs until the actual
  1708. X * rpc call times out.
  1709. X *
  1710. X * sendsz and recvsz are the maximum allowable packet sizes that can be
  1711. X * sent and received.
  1712. X */
  1713. XCLIENT *
  1714. Xclntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
  1715. X    struct sockaddr_in *raddr;
  1716. X    u_long program;
  1717. X    u_long version;
  1718. X    struct timeval wait;
  1719. X    register int *sockp;
  1720. X    u_int sendsz;
  1721. X    u_int recvsz;
  1722. X{
  1723. X    CLIENT *cl;
  1724. X    register struct cu_data *cu;
  1725. X    struct timeval now;
  1726. X    struct rpc_msg call_msg;
  1727. X
  1728. X    cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
  1729. X    if (cl == NULL) {
  1730. X        fprintf(stderr, "clntudp_create: out of memory\n");
  1731. X        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1732. X        rpc_createerr.cf_error.re_errno = errno;
  1733. X        goto fooy;
  1734. X    }
  1735. X    sendsz = ((sendsz + 3) / 4) * 4;
  1736. X    recvsz = ((recvsz + 3) / 4) * 4;
  1737. X    cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
  1738. X    if (cu == NULL) {
  1739. X        fprintf(stderr, "clntudp_create: out of memory\n");
  1740. X        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1741. X        rpc_createerr.cf_error.re_errno = errno;
  1742. X        goto fooy;
  1743. X    }
  1744. X    cu->cu_outbuf = &cu->cu_inbuf[recvsz];
  1745. X
  1746. X    (void)gettimeofday(&now, (struct timezone *)0);
  1747. X    if (raddr->sin_port == 0) {
  1748. X        u_short port;
  1749. X        if ((port =
  1750. X            pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  1751. X            goto fooy;
  1752. X        }
  1753. X        raddr->sin_port = htons(port);
  1754. X    }
  1755. X    cl->cl_ops = &udp_ops;
  1756. X    cl->cl_private = (caddr_t)cu;
  1757. X    cu->cu_raddr = *raddr;
  1758. X    cu->cu_rlen = sizeof (cu->cu_raddr);
  1759. X    cu->cu_wait = wait;
  1760. X    cu->cu_sendsz = sendsz;
  1761. X    cu->cu_recvsz = recvsz;
  1762. X    call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  1763. X    call_msg.rm_direction = CALL;
  1764. X    call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  1765. X    call_msg.rm_call.cb_prog = program;
  1766. X    call_msg.rm_call.cb_vers = version;
  1767. X    xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
  1768. X        sendsz, XDR_ENCODE);
  1769. X    if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
  1770. X        goto fooy;
  1771. X    }
  1772. X    cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  1773. X    if (*sockp < 0) {
  1774. X        int dontblock = 1;
  1775. X
  1776. X        *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1777. X        if (*sockp < 0) {
  1778. X            rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  1779. X            rpc_createerr.cf_error.re_errno = errno;
  1780. X            goto fooy;
  1781. X        }
  1782. X        /* the sockets rpc controls are non-blocking */
  1783. X        (void)ioctl(*sockp, FIONBIO, &dontblock);
  1784. X    }
  1785. X    cu->cu_sock = *sockp;
  1786. X    cl->cl_auth = authnone_create();
  1787. X    return (cl);
  1788. Xfooy:
  1789. X    if (cu)
  1790. X        mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
  1791. X    if (cl)
  1792. X        mem_free((caddr_t)cl, sizeof(CLIENT));
  1793. X    return ((CLIENT *)NULL);
  1794. X}
  1795. X
  1796. XCLIENT *
  1797. Xclntudp_create(raddr, program, version, wait, sockp)
  1798. X    struct sockaddr_in *raddr;
  1799. X    u_long program;
  1800. X    u_long version;
  1801. X    struct timeval wait;
  1802. X    register int *sockp;
  1803. X{
  1804. X
  1805. X    return(clntudp_bufcreate(raddr, program, version, wait, sockp,
  1806. X        UDPMSGSIZE, UDPMSGSIZE));
  1807. X}
  1808. X
  1809. Xstatic enum clnt_stat 
  1810. Xclntudp_call(cl, proc, xargs, argsp, xresults, resultsp, timeout)
  1811. X    register CLIENT    *cl;        /* client handle */
  1812. X    u_long        proc;        /* procedure number */
  1813. X    xdrproc_t    xargs;        /* xdr routine for args */
  1814. X    caddr_t        argsp;        /* pointer to args */
  1815. X    xdrproc_t    xresults;    /* xdr routine for results */
  1816. X    caddr_t        resultsp;    /* pointer to results */
  1817. X    struct timeval    timeout;    /* seconds to wait before giving up */
  1818. X{
  1819. X    register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  1820. X    register XDR *xdrs;
  1821. X    register int outlen;
  1822. X    register int inlen;
  1823. X    int readfds, fromlen;
  1824. X    register int mask;
  1825. X    struct sockaddr_in from;
  1826. X    struct rpc_msg reply_msg;
  1827. X    XDR reply_xdrs;
  1828. X    struct timeval time_waited;
  1829. X    bool_t ok;
  1830. X
  1831. Xcall_again:
  1832. X    time_waited.tv_sec = 0;
  1833. X    time_waited.tv_usec = 0;
  1834. X    xdrs = &(cu->cu_outxdrs);
  1835. X    xdrs->x_op = XDR_ENCODE;
  1836. X    XDR_SETPOS(xdrs, cu->cu_xdrpos);
  1837. X    /*
  1838. X     * the transaction is the first thing in the out buffer
  1839. X     */
  1840. X    (*(u_short *)(cu->cu_outbuf))++;
  1841. X    if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  1842. X        (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
  1843. X        (! (*xargs)(xdrs, argsp)))
  1844. X        return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  1845. X    outlen = (int)XDR_GETPOS(xdrs);
  1846. X    while (TRUE) {
  1847. X
  1848. X        if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  1849. X            (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
  1850. X            != outlen) {
  1851. X            cu->cu_error.re_errno = errno;
  1852. X            return (cu->cu_error.re_status = RPC_CANTSEND);
  1853. X        }
  1854. X        /*
  1855. X         * sub-optimal code appears inside the loop because we have
  1856. X         * some clock time to spare while the packets are in flight.
  1857. X         * (We assume that this is actually only executed once.)
  1858. X         */
  1859. X        reply_msg.acpted_rply.ar_verf = _null_auth;
  1860. X        reply_msg.acpted_rply.ar_results.where = resultsp;
  1861. X        reply_msg.acpted_rply.ar_results.proc = xresults;
  1862. X        mask = 1 << cu->cu_sock;
  1863. Xrcv_again:
  1864. X        readfds = mask;
  1865. X        switch (select(32, &readfds, (int *)NULL, (int *)NULL,
  1866. X            &(cu->cu_wait))) {
  1867. X
  1868. X        case 0:
  1869. X            time_waited.tv_sec += cu->cu_wait.tv_sec;
  1870. X            time_waited.tv_usec += cu->cu_wait.tv_usec;
  1871. X            while (time_waited.tv_usec >= 1000000) {
  1872. X                time_waited.tv_sec++;
  1873. X                time_waited.tv_usec -= 1000000;
  1874. X            }
  1875. X            if ((time_waited.tv_sec < timeout.tv_sec) ||
  1876. X                ((time_waited.tv_sec == timeout.tv_sec) &&
  1877. X                (time_waited.tv_usec < timeout.tv_usec)))
  1878. X                continue;
  1879. X            return (cu->cu_error.re_status = RPC_TIMEDOUT);
  1880. X
  1881. X        case -1:
  1882. X            if (errno == EINTR)
  1883. X                goto rcv_again;
  1884. X            cu->cu_error.re_errno = errno;
  1885. X            return (cu->cu_error.re_status = RPC_CANTRECV);
  1886. X        }
  1887. X        if ((readfds & mask) == 0)
  1888. X            goto rcv_again;
  1889. Xtryagain:
  1890. X        fromlen = sizeof(struct sockaddr);
  1891. X        inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, cu->cu_recvsz, 0,
  1892. X            (struct sockaddr *)&from, &fromlen);
  1893. X        if (inlen < 0) {
  1894. X            if (errno == EINTR)
  1895. X                goto tryagain;
  1896. X            if (errno == EWOULDBLOCK)
  1897. X                goto rcv_again;
  1898. X            cu->cu_error.re_errno = errno;
  1899. X            return (cu->cu_error.re_status = RPC_CANTRECV);
  1900. X        }
  1901. X        if (inlen < sizeof(u_long))
  1902. X            goto rcv_again;
  1903. X        /* see if reply transaction id matches sent id */
  1904. X        if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf)))
  1905. X            goto rcv_again;
  1906. X        /* we now assume we have the proper reply */
  1907. X        break;
  1908. X    }
  1909. X
  1910. X    /*
  1911. X     * now decode and validate the response
  1912. X     */
  1913. X    xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
  1914. X    ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  1915. X    /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
  1916. X    if (ok) {
  1917. X        _seterr_reply(&reply_msg, &(cu->cu_error));
  1918. X        if (cu->cu_error.re_status == RPC_SUCCESS) {
  1919. X            if (! AUTH_VALIDATE(cl->cl_auth,
  1920. X                &reply_msg.acpted_rply.ar_verf)) {
  1921. X                cu->cu_error.re_status = RPC_AUTHERROR;
  1922. X                cu->cu_error.re_why = AUTH_INVALIDRESP;
  1923. X            }
  1924. X            if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  1925. X                xdrs->x_op = XDR_FREE;
  1926. X                (void)xdr_opaque_auth(xdrs,
  1927. X                    &(reply_msg.acpted_rply.ar_verf));
  1928. X            } 
  1929. X        }  /* end successful completion */
  1930. X        else {
  1931. X            /* maybe our credentials need to be refreshed ... */
  1932. X            if (AUTH_REFRESH(cl->cl_auth))
  1933. X                goto call_again;
  1934. X        }  /* end of unsuccessful completion */
  1935. X    }  /* end of valid reply message */
  1936. X    else {
  1937. X        cu->cu_error.re_status = RPC_CANTDECODERES;
  1938. X    }
  1939. X    return (cu->cu_error.re_status);
  1940. X}
  1941. X
  1942. Xstatic void
  1943. Xclntudp_geterr(cl, errp)
  1944. X    CLIENT *cl;
  1945. X    struct rpc_err *errp;
  1946. X{
  1947. X    register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  1948. X
  1949. X    *errp = cu->cu_error;
  1950. X}
  1951. X
  1952. X
  1953. Xstatic bool_t
  1954. Xclntudp_freeres(cl, xdr_res, res_ptr)
  1955. X    CLIENT *cl;
  1956. X    xdrproc_t xdr_res;
  1957. X    caddr_t res_ptr;
  1958. X{
  1959. X    register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  1960. X    register XDR *xdrs = &(cu->cu_outxdrs);
  1961. X
  1962. X    xdrs->x_op = XDR_FREE;
  1963. X    return ((*xdr_res)(xdrs, res_ptr));
  1964. X}
  1965. X
  1966. Xstatic void 
  1967. Xclntudp_abort(/*h*/)
  1968. X    /*CLIENT *h;*/
  1969. X{
  1970. X}
  1971. X
  1972. Xstatic void
  1973. Xclntudp_destroy(cl)
  1974. X    CLIENT *cl;
  1975. X{
  1976. X    register struct cu_data *cu = (struct cu_data *)cl->cl_private;
  1977. X
  1978. X    XDR_DESTROY(&(cu->cu_outxdrs));
  1979. X    mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
  1980. X    mem_free((caddr_t)cl, sizeof(CLIENT));
  1981. X}
  1982. SHAR_EOF
  1983. if test 10438 -ne "`wc -c < 'rpc/rpclib/clnt_udp.c'`"
  1984. then
  1985.     echo shar: "error transmitting 'rpc/rpclib/clnt_udp.c'" '(should have been 10438 characters)'
  1986. fi
  1987. chmod 444 'rpc/rpclib/clnt_udp.c'
  1988. fi
  1989. exit 0
  1990. #    End of shell archive
  1991.